import 'package:flutter/material.dart';
import 'package:niuren_demo/configs/App_style_config.dart';
import 'package:niuren_demo/configs/Colors_util.dart';
import 'package:niuren_demo/page/assets/assets_page.dart';
import 'package:niuren_demo/page/dynamic/dynamic_page.dart';
import 'package:niuren_demo/page/mine/mine_page.dart';
import 'package:niuren_demo/page/niuren/niuren_page.dart';


class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  List<Widget> pages;

  List<Map> navItems = [
    {"name": "牛人","icon":Icons.home},
    {"name": "动态","icon":Icons.wb_cloudy},
    {"name": "资产","icon":Icons.attach_money_sharp},
    {"name": "我的","icon":Icons.perm_identity_outlined},
  ];

  List<BottomNavigationBarItem> bottomNavItems = List();
  PageController pageController = PageController();

  int currentIndex = 1;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    initBottomNavItems();
    initPages();
  }

  // 初始化页面
  initPages() {
    pages = [
      NiurenPage(),
      DynamicPage(),
      AssetsPage(),
      MinePage(),
    ];
  }

  // 初始化NavItems
  initBottomNavItems() {
    navItems.forEach((element) {
      BottomNavigationBarItem itme = BottomNavigationBarItem(
        icon: Container(
          width: 24,
          height: 24,
          child:Icon(element["icon"]),
          // Image(image: AssetImage(element["imageurl"]),),
        ),
        label: element["name"],
      );
      bottomNavItems.add(itme);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: Theme(
        data: Theme.of(context).copyWith(
          canvasColor: Colors.white,
        ),
        child: BottomNavigationBar(
          unselectedItemColor: Colors.black,
          selectedItemColor: AppStyleConfig.themColor,
          showUnselectedLabels: true,
          items: bottomNavItems,
          type: BottomNavigationBarType.fixed,
          currentIndex: currentIndex,
          onTap: (index) {
            currentIndex = index;
            pageController.animateToPage(currentIndex, duration: new Duration(seconds: 2),curve:new ElasticOutCurve(0.8));
            setState(() {});
          },
        ),
      ),
      body: PageView(
          children: pages,
          physics: NeverScrollableScrollPhysics(),
          controller: pageController),
    );
  }
}